/*
 * filemanager.h
 *
 *      Author: Jerome RD Soine and Christoph A Brand
 *      Institution: Schwarz goup, Institute for Theoretical Physics, Heidelberg University, Germany
 */

#ifndef FILEMANAGER_H_
#define FILEMANAGER_H_

#include "../base/triangle.h"
#include "../base/edge.h"
#include "../base/vertex.h"
#include "vtk_color/colordataextractor.h"
#include "vtk_color/fixedpointsextractor.h"
#include "../geomobject.h"

#include <string>
#include <vector>
#include <list>

using namespace std;

class FileManager {

private:
	static const int cell_type_triangle = 5;
	static const int cell_type_line = 3;
	static const int number_of_tips_of_a_triangle = 3;
	static const int number_of_tips_of_a_line = 2;
	static const int number_of_borders_of_a_triangle = 3;
	static const int number_of_points_in_a_triangle_data_line = 4;
	static const int number_of_points_in_a_line_data_line = 3;

	GeomObject * g;
	int numberOfSavedStates;
	string snapshotPath;
	ColorDataExtractor * cde;
	FixedPointsExtractor * fpe;


	void loadVTK(ifstream & fin);
	void initializeVerticesFromVTK(ifstream & fin, int numberofvertices);
	void initializeCellsFromVTK(ifstream & fin, int numberofcells);
	void initializeStressFibresFromVTK(ifstream & fin, int numberofcells);
	void initializeFixedPointsFromVTK(ifstream & fin, int numberofpoints);
	Triangle* createTriangle(vector<Vertex*> tips, vector<Edge*> & edgeList, bool firstTriangle);
	void initializeEdges(vector<Edge*> & edgeList);
	void initializeTriangles(vector<Triangle*> & triangleList);
	Vertex* createVertexFromVTKString(string vertex);
	Edge* getOrCreateEdgeConnectingVertices(Vertex* v1, Vertex* v2, vector<Edge*> & edgelist);

	void saveLineVTK(ofstream & fout);
	void saveVTK(ofstream & fout);
	void printVTKHeader(ofstream & fout);
	void printVTKPoints(ofstream & fout);
	void printVTKCells(ofstream & fout);
	void printVTKTriangles(ofstream & fout);
	void printVTKLines(ofstream & fout);
	void printVTKTriangle(ofstream & fout, Triangle * t);
	void printVTKLine(ofstream & fout, Edge * e);

	void clearSnapshotDirectory();

	bool edgeOrientedCorrectly(Edge* e, Vertex * tail, Vertex * head){return e->getHead()==head&&e->getTail()==tail;};
	vector<Vertex*> read2TipsFromVTK(istringstream & icell, vector<Vertex*>* & vertices);
	vector<Vertex*> read3TipsFromVTK(istringstream & icell, vector<Vertex*>* & vertices);

public:
	FileManager () {};
	FileManager ( GeomObject * G, string SNAPSHOT_PATH, bool clear);
	void load(string filename);
	void save(string filename);
	void saveLines(string filename);
	void saveSnapshot();

	void setColorDataExtractor(ColorDataExtractor * CDE){cde = CDE;};
	void setFixedPointsExtractor(FixedPointsExtractor * FPE){fpe = FPE;};
};

#endif /* FILEMANAGER_H_ */
